<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>轮播图的实现</title>
  <style>
    * {
      padding: 0;
      margin: 0;
      background-color: black;
    }

    .container {
      position: relative;
      width: 1000px;
      height: 800px;
      background-color: white;
      left: 600px;
      top: 100px;
    }
    
    .image {
      display: block;
      width: 1000px;
      height: 800px;
    }

    #leftArrow {
      width: 50px;
      height: 50px;
      text-align: center;
      line-height: 50px;
      background-color: pink;
      position: absolute;
      top: 50%;
      cursor: pointer;
    }
    
    #rightArrow {
      width: 50px;
      height: 50px;
      text-align: center;
      line-height: 50px;
      background-color: pink;
      position: absolute;
      top: 50%;
      right: 0;
      cursor: pointer;
    }
  </style>
</head>
<body>
  <div class="container">
    <img class="image" src="./images/图1.png" alt="">
    <div id="leftArrow"> < </div>
    <div id="rightArrow"> > </div>
  </div>
  <script>
    const img = document.getElementsByTagName('img')
    let leftBtn = document.getElementById('leftArrow')
    let rightBtn = document.getElementById('rightArrow')
    let index = 1
    leftBtn.onclick = () => {
      index++
      timer()
    }
    rightBtn.onclick = () => {
      index--
      timer()
    }
    const timer = (time) => {
      index++
      if (index > 4) {
        index = 1
      }
      img[0].src = `./images/图${index}.png`
    }
    setInterval(timer, 2000)
  </script>
</body>
</html>